//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "PercentageStrikePayoff.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/StrikedTypePayoff.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CPercentageStrikePayoff::CPercentageStrikePayoff (QL::Option::TypeEnum type, Double moneyness) : CStrikedTypePayoff(CPercentageStrikePayoff::typeid)
{
    try
    {
#ifdef HANDLE
        _phPercentageStrikePayoff = NULL;
#endif
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        QuantLib::Real _moneyness = (QuantLib::Real)ValueHelper::Convert (moneyness);
        _ppPercentageStrikePayoff = new boost::shared_ptr<QuantLib::PercentageStrikePayoff> (new QuantLib::PercentageStrikePayoff ( _type,  _moneyness ));
        SetStrikedTypePayoff (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppPercentageStrikePayoff));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::CPercentageStrikePayoff::CPercentageStrikePayoff (boost::shared_ptr<QuantLib::PercentageStrikePayoff>& childNative, Object^ owner) : CStrikedTypePayoff(CPercentageStrikePayoff::typeid)
{
#ifdef HANDLE
	_phPercentageStrikePayoff = NULL;
#endif
	_ppPercentageStrikePayoff = &childNative;
    _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppPercentageStrikePayoff));
}
Cephei::QL::Instruments::CPercentageStrikePayoff::CPercentageStrikePayoff (QuantLib::PercentageStrikePayoff& childNative, Object^ owner) : CStrikedTypePayoff(CPercentageStrikePayoff::typeid)
{
#ifdef HANDLE
	_phPercentageStrikePayoff = NULL;
#endif
	_ppPercentageStrikePayoff = new boost::shared_ptr<QuantLib::PercentageStrikePayoff> (&childNative);
    _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppPercentageStrikePayoff));
    _PercentageStrikePayoffOwner = owner;
    _StrikedTypePayoffOwner = owner;
}

Cephei::QL::Instruments::CPercentageStrikePayoff::CPercentageStrikePayoff (CPercentageStrikePayoff^ copy) : CStrikedTypePayoff(CPercentageStrikePayoff::typeid)
{
#ifdef HANDLE
	_phPercentageStrikePayoff = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPercentageStrikePayoff = new boost::shared_ptr<QuantLib::PercentageStrikePayoff> (copy->GetShared());
        _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppPercentageStrikePayoff));
    }
}
Cephei::QL::Instruments::CPercentageStrikePayoff::CPercentageStrikePayoff (System::Type^ t) : CStrikedTypePayoff(CPercentageStrikePayoff::typeid)
{
#ifdef HANDLE
	_phPercentageStrikePayoff = NULL;
#endif
	if (!t->IsSubclassOf(CPercentageStrikePayoff::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CPercentageStrikePayoff::CPercentageStrikePayoff (QuantLib::Handle<QuantLib::PercentageStrikePayoff>& childNative, Object^ owner)  : CStrikedTypePayoff(CPercentageStrikePayoff::typeid)
{
	_phPercentageStrikePayoff = &childNative;
	_ppPercentageStrikePayoff = &static_cast<boost::shared_ptr<QuantLib::PercentageStrikePayoff>>(childNative.currentLink());
    _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppPercentageStrikePayoff));
    _PercentageStrikePayoffOwner = owner;
}
Cephei::QL::Instruments::CPercentageStrikePayoff::CPercentageStrikePayoff (QuantLib::Handle<QuantLib::PercentageStrikePayoff> childNative)  : CStrikedTypePayoff(CPercentageStrikePayoff::typeid)
{
	_phPercentageStrikePayoff = &childNative;
	_ppPercentageStrikePayoff = &static_cast<boost::shared_ptr<QuantLib::PercentageStrikePayoff>>(childNative.currentLink());
    _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppPercentageStrikePayoff));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CPercentageStrikePayoff::CPercentageStrikePayoff (QuantLib::PercentageStrikePayoff childNative)  : CStrikedTypePayoff(CPercentageStrikePayoff::typeid)
{
#ifdef HANDLE
	_phPercentageStrikePayoff = NULL;
#endif
	_ppPercentageStrikePayoff = new boost::shared_ptr<QuantLib::PercentageStrikePayoff> (new QuantLib::PercentageStrikePayoff (childNative));
    _ppStrikedTypePayoff = new boost::shared_ptr<QuantLib::StrikedTypePayoff> (boost::dynamic_pointer_cast<QuantLib::StrikedTypePayoff> (*_ppPercentageStrikePayoff));
}
#endif

Cephei::QL::Instruments::CPercentageStrikePayoff::~CPercentageStrikePayoff ()
{
    if (_ppPercentageStrikePayoff != NULL)
    {
	    delete _ppPercentageStrikePayoff;
        _ppPercentageStrikePayoff = NULL;
    }
}
Cephei::QL::Instruments::CPercentageStrikePayoff::!CPercentageStrikePayoff ()
{
    if (_ppPercentageStrikePayoff != NULL)
    {
	    delete _ppPercentageStrikePayoff;
    }
}
QuantLib::PercentageStrikePayoff& Cephei::QL::Instruments::CPercentageStrikePayoff::GetReference ()
{
    if (_ppPercentageStrikePayoff == NULL) throw gcnew NativeNullException ();
	return **_ppPercentageStrikePayoff;
}
boost::shared_ptr<QuantLib::PercentageStrikePayoff>& Cephei::QL::Instruments::CPercentageStrikePayoff::GetShared ()
{
    if (_ppPercentageStrikePayoff == NULL) throw gcnew NativeNullException ();
	return *_ppPercentageStrikePayoff;
}
QuantLib::PercentageStrikePayoff* Cephei::QL::Instruments::CPercentageStrikePayoff::GetPointer ()
{
    if (_ppPercentageStrikePayoff == NULL) throw gcnew NativeNullException ();
	return &**_ppPercentageStrikePayoff;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PercentageStrikePayoff>& Cephei::QL::Instruments::CPercentageStrikePayoff::GetHandle ()
{
	if (_phPercentageStrikePayoff == NULL)
	{
		_phPercentageStrikePayoff = new Handle<QuantLib::PercentageStrikePayoff> (*_ppPercentageStrikePayoff);
	}
	return *_phPercentageStrikePayoff;
}
#endif
bool Cephei::QL::Instruments::CPercentageStrikePayoff::HasNative () 
{
	return (_ppPercentageStrikePayoff != NULL);
}

String^ Cephei::QL::Instruments::CPercentageStrikePayoff::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppPercentageStrikePayoff)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IPercentageStrikePayoff^ Cephei::QL::Instruments::CPercentageStrikePayoff_Factory::Create (QL::Option::TypeEnum type, Double moneyness)
{
    return gcnew CPercentageStrikePayoff ( type,  moneyness);
}
